<?php
//
//	file: includes/calendar/topics/class_calendar_topics_plugs.php
//	author: ptirhiik
//	begin: 03/02/2007
//	version: 1.6.0 - 03/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

// admin/admin_icons
class calendar_admin_icons extends plug_ins
{
	function types(&$list_types)
	{
		$list_types[POST_CALENDAR] = 'Calendar';
		return true;
	}
}

// includes/class_forum
class calendar_forums extends plug_ins
{
	function auths_def(&$auths_def)
	{
		$auths_def += array(
			'auth_calendar' => array('can' => 'Rules_calendar_can', 'cannot' => 'Rules_calendar_cannot'),
		);
		return true;
	}
}

// includes/class_posts
class calendar_posts extends plug_ins
{
	function topic_fields(&$topic_fields)
	{
		$topic_fields[] = 'topic_calendar_time';
		$topic_fields[] = 'topic_calendar_duration';
		return true;
	}
}

// includes/class_topics
class calendar_front_title
{
	function front_title($tpl_level, &$row, $first_post=false)
	{
		global $template;

		$res = false;
		if ( $first_post )
		{
			$template->assign_vars(array(
				'L_CALENDAR_EVENT' => $template->lang('Calendar_event'),
			));
			$res = isset($row['topic_calendar_time']) && intval($row['topic_calendar_time']);
			$template->assign_block_vars((empty($tpl_level) ? '' : $tpl_level . '.') . 'calendar_event' . (!$res ? '_ELSE' : ''), !$res ? array() : array(
				'S_CALENDAR_EVENT' => $this->title(intval($row['topic_calendar_time']), isset($row['topic_calendar_duration']) ? intval($row['topic_calendar_duration']) : 0),
			));
		}
		return $res;
	}

	function title($time, $duration)
	{
		global $user, $config;

		if ( $time )
		{
			$config->globals['mod_topic_calendar_CH']->api->set();
			$xstart = $config->globals['mod_topic_calendar_CH']->api->sys_to_user($time);
			$nb = 0;
			$xend = array();
			$fmt = $xstart['h'] || $xstart['i'] || $xstart['s'] ? $config->globals['mod_topic_calendar_CH']->api->format_long : $config->globals['mod_topic_calendar_CH']->api->format_medium;
			$duration_user = $duration + 1;
			if ( empty($duration) )
			{
				$xend = $xstart;
				$lang_key = 'Calendar_from';
			}
			else if ( ($duration_user < 86400) && !($duration_user % 3600) )
			{
				$nb = intval($duration_user / 3600);
				$lang_key = $nb == 1 ? 'Calendar_one_hour' : 'Calendar_many_hours';
			}
			else if ( !($duration_user % 86400) )
			{
				$nb = intval($duration_user / 86400);
				$lang_key = $nb == 1 ? 'Calendar_one_day' : 'Calendar_many_days';
			}
			else
			{
				$xend = $config->globals['mod_topic_calendar_CH']->api->adjust_time($xstart['h'], $xstart['i'], $xstart['s'] + $duration, $xstart['m'], $xstart['d'], $xstart['y']);
				$fmt = $xstart['h'] || $xstart['i'] || $xstart['s'] || $xend['h'] || $xend['i'] || $xsend['s'] ? $config->globals['mod_topic_calendar_CH']->api->format_long : $config->globals['mod_topic_calendar_CH']->api->format_medium;
				$lang_key = 'Calendar_from_to';
			}
		}
		return $time ? sprintf($user->lang($lang_key), $config->globals['mod_topic_calendar_CH']->api->date($fmt, $xstart), empty($xend) ? $nb : $config->globals['mod_topic_calendar_CH']->api->date($fmt, $xend)) : false;
	}
}

// includes/page_header
class calendar_header extends plug_ins
{
	function start()
	{
		global $config, $template, $user;
		global $gen_simple_header;

		// menu
		$template->assign_vars(array(
			'U_CALENDAR' => $config->url('calendar', '', true),
			'L_CALENDAR' => $template->lang('Calendar'),
			'I_CALENDAR' => $template->img('menu_calendar'),
			'L_CALENDAR_ALT' => $template->lang('Calendar_event'),
		));
		$template->set_switch('topic_calendar');

		if ( !defined('HAS_DIED') && !defined('IN_CALENDAR') && !defined('IN_LOGIN') && !defined('IN_INSTALL') && !$gen_simple_header )
		{
			if ( (isset($config->data['calendar_header_cells']) && intval($config->data['calendar_header_cells'])) || (isset($user->data['user_calendar_header_cells']) && intval($user->data['user_calendar_header_cells']) && !(isset($config->data['calendar_header_cells_over']) && intval($config->data['calendar_header_cells_over']))) )
			{
				include($config->url('includes/calendar/class_calendar_handler'));
				$calendar_header_box = new calendar_header_box(INDEX);
				$calendar_header_box->display();
				$calendar_header_box->destroy();
				unset($calendar_header_box);
			}
		}
		return true;
	}
}

?>